-- test2.lua

function read_file_numbers(filename)
    local array = {}
    local f = assert(io.open(filename))
    while true do
        local v = f:read("*number")
        if not v then break end
        table.insert(array, v)
    end
    f:close()
    return array
end

function write_file_numbers(filename, array)
    local f = assert(io.open(filename, 'w'))
    for _,v in ipairs(array) do
        f:write(v, ' ')
    end
    f:close()
end

-- Create array in memory
local t = {3,4,5,2,5,3e20,10,2,7,-0.3}

-- Write array to file
write_file_numbers("file.dat", t)

-- Read array back from file
local t2 = read_file_numbers("file.dat")

-- Check that array is unchanged
for i=1,#t do
    assert(t[i] == t2[i])
end
print("ok")

